//------------------------------------------------------------------------------
//
// 客户接待表单功能
// name: consultant_client_reception_d
// author: 喵大斯( as3er.net )
// created: 2015/8/24
// copyright (c) 2015 喵大斯( aosnow@yeah.net )
//
//------------------------------------------------------------------------------

/// <reference path='../core/jquery.d.ts'/>
/// <reference path='../core/page_core.d.ts'/>
/// <reference path='ClientData.ts'/>
/// <reference path='ClientReserve.ts'/>
/// <reference path='ClientReception.ts'/>

//--------------------------------------------------------------------------
//
// Class variables
//
//--------------------------------------------------------------------------

/** 外部初始化方法 */
declare var init:Function;

// 接待表单的类型（1：有预约，2：无预约）
declare var recType:number;

// 客户 Key 唯一值
declare var clientKey:string;

// 当接待类型为有预约时的预约编号 key
declare var reserveKey:string;

// 当前页面所查询的接待数据
declare var modifydata:any;

// 客户资料显示控制器
var cdd:ClientData;

// 预约数据显示控制器
var crd:ClientReserve;

// 接待表单处理器
var receptionor:ClientReception;

//--------------------------------------------------------------------------
//
// Class methods
//
//--------------------------------------------------------------------------

// 初始化
$( "body" ).ready( pageInit );

// 页面初始化
function pageInit()
{
	var type:string = recType == 1 ? ClientReception.TYPE_WITH_RESERVE : ClientReception.TYPE_WITHOUT_RESERVE;

	// 键盘事件
	$( "body" ).bind( "keyup", bodyKeyupHandler );

	// 创建客户资料显示器
	var tbl_cli:JQuery = $( "#tbl_client" );
	cdd = new ClientData( tbl_cli, type == ClientReception.TYPE_WITHOUT_RESERVE );
	if( type == ClientReception.TYPE_WITHOUT_RESERVE )
	{
		cdd.initTableForWithoutReserve();
		tbl_cli.bind( ClientData.SELECTED, selectClientHandler );
	}
	cdd.detailed( false );

	// 创建预约资料显示器
	var tbl_res:JQuery = $( "#tbl_reserve" );
	if( type == ClientReception.TYPE_WITHOUT_RESERVE )
	{
		tbl_res.hide();
	}
	else
	{
		crd = new ClientReserve( tbl_res );
	}

	// 创建接待表单处理器
	receptionor = new ClientReception( $( "#tbl_reception" ), type );

	// 数据初始化（修改接待时）
	dataInit();

	// 若外部需要设置初始化操作，则实现 init 名称的方法，即可被自动调用
	if( typeof( init ) == "function" ) init();

	// 打开查询预约记录窗口
	$( "#btnSelectReserve" ).click( reserveOpenHandler );

	// 表单提交
	$( "#btnSubmit" ).click( submitHandler );
}

function dataInit()
{
	if( !empty( reserveKey ) )
	{
		crd.reserveKey = reserveKey;
		crd.requestInfo( dataInitOther );
	}
	else
	{
		cdd.clientKey = clientKey;
		cdd.request_client_info();

		receptionor.clientKey = clientKey;
		//receptionor.resetHtmlContent();
		receptionor.setReception( modifydata );
	}
}

function dataInitOther( d )
{
	crd.setData( d );
	cdd.clientKey = crd.clientKey;
	cdd.request_client_info();

	receptionor.clientKey = crd.clientKey;
	//receptionor.resetHtmlContent();
	receptionor.setReception( modifydata );
}

//--------------------------------------------------------------------------
//
// Event handlers
//
//--------------------------------------------------------------------------

function bodyKeyupHandler( e )
{
	if( e.ctrlKey && e.which == 13 )
	{
		submitHandler();
	}
}

function reserveOpenHandler()
{
	reserver.reset();
	reserver.setCallback( selectReserveHandler );
	reserver.open();
}

function selectReserveHandler( d )
{
	crd.setData( d );
	cdd.clientKey = crd.clientKey;
	cdd.request_client_info();

	receptionor.clientKey = crd.clientKey;
	receptionor.resetHtmlContent();
}

function selectClientHandler()
{
	receptionor.clientKey = cdd.clientKey;
	receptionor.resetHtmlContent();
}

function submitHandler()
{
	// 客户key
	if( empty( receptionor.clientKey ) )
	{
		infopad.err( "未指定接待的目标客户。" );
		return;
	}

	// 来自于接待表单处理程序的数据
	var data:any = receptionor.getFormData();
	data.rkey = empty( crd ) ? "" : crd.reserveKey;
	data.newver = 1;

	submitter.post( "consultant_client_reception_go.php", data, function( d )
	{
		if( d.err == 0 )
		{
			infopad.msg( d.msg );
			receptionor.receptionKey = d.key;
		}
		else
		{
			infopad.err( d.msg, d.delay );
		}
	}, "json" );
}